"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const geoserver_js_1 = require("./util/geoserver.js");
class SecurityClient {
    constructor(url, auth) {
        this.url = url;
        this.auth = auth;
    }
    async getAllUsers() {
        const response = await fetch(this.url + 'security/usergroup/users.json', {
            credentials: 'include',
            method: 'GET',
            headers: {
                Authorization: this.auth
            }
        });
        if (!response.ok) {
            const geoServerResponse = await (0, geoserver_js_1.getGeoServerResponseText)(response);
            throw new geoserver_js_1.GeoServerResponseError(null, geoServerResponse);
        }
        return response.json();
    }
    async createUser(username, password) {
        const body = {
            user: {
                userName: username,
                password: password,
                enabled: true
            }
        };
        const response = await fetch(this.url + 'security/usergroup/users.json', {
            credentials: 'include',
            method: 'POST',
            headers: {
                Authorization: this.auth,
                'content-type': 'application/json'
            },
            body: JSON.stringify(body)
        });
        if (!response.ok) {
            const geoServerResponse = await (0, geoserver_js_1.getGeoServerResponseText)(response);
            switch (response.status) {
                case 404:
                    throw new geoserver_js_1.GeoServerResponseError(`User ${username} might already exists.`, geoServerResponse);
                default:
                    throw new geoserver_js_1.GeoServerResponseError(null, geoServerResponse);
            }
        }
    }
    async updateUser(username, password, enabled) {
        const body = {
            user: {
                password: password,
                enabled: enabled
            }
        };
        const response = await fetch(this.url + 'security/usergroup/user/' + username, {
            credentials: 'include',
            method: 'POST',
            headers: {
                Authorization: this.auth,
                'content-type': 'application/json'
            },
            body: JSON.stringify(body)
        });
        if (!response.ok) {
            const geoServerResponse = await (0, geoserver_js_1.getGeoServerResponseText)(response);
            throw new geoserver_js_1.GeoServerResponseError(null, geoServerResponse);
        }
    }
    async associateUserRole(username, role) {
        const response = await fetch(`${this.url}security/roles/role/${role}/user/${username}`, {
            credentials: 'include',
            method: 'POST',
            headers: {
                Authorization: this.auth
            }
        });
        if (!response.ok) {
            const geoServerResponse = await (0, geoserver_js_1.getGeoServerResponseText)(response);
            throw new geoserver_js_1.GeoServerResponseError(null, geoServerResponse);
        }
    }
}
exports.default = SecurityClient;
